﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 清除标题文本命令。
    /// ★①，修改类名。
    /// </summary>
    public static class SwitchTitleCollapsedCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static SwitchTitleCollapsedCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand("SwitchTitleCollapsedCommand", "SwitchTitleCollapsedCommand",
                typeof(SwitchTitleCollapsedCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.C,ModifierKeys.Alt,"Alt+C")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            SwitchTitleCollapsed();
        }

        /// <summary>
        /// 切换折叠/展开状态。
        /// </summary>
        internal static void SwitchTitleCollapsed()
        {
            if (Globals.SelectedTitlesList.Count != 1)
            {
                MessageBox.Show("只能对一个标题执行“折叠/展开”操作！", Globals.AppName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Title t = Globals.SelectedTitlesList[0];
            if (t.HigherLevelTitleID == null || t.HigherLevelTitleID.Length <= 0)
            {
                MessageBox.Show("根标题不能进行“折叠/展开”操作！", Globals.AppName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            List<Title> lowers = t.GetLowerLevelTitles();
            if (lowers == null || lowers.Count <= 0)
            {
                MessageBox.Show("此标题没有下级标题，“折叠｜展开”操作无意义！", Globals.AppName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "切换展开｜折叠"
            };

            Action act = new Action(t.ID, -1, t.GetType().Name,
                "CollapsedLowers", t.CollapsedLowers.ToString(), (!t.CollapsedLowers).ToString());

            t.CollapsedLowers = !t.CollapsedLowers;

            mi.Add(act);

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.RefreshTitleAndLinesLocation();
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            if (Globals.SelectedTitlesList.Count == 1)
            {
                Title title = Globals.SelectedTitlesList[0];
                if (title.HigherLevelTitleID == null || title.HigherLevelTitleID.Length <= 0)
                {
                    e.CanExecute = false;//根标题不允许折叠。
                    return;
                }
                else
                {
                    e.CanExecute = true;
                    return;
                }
            }
            else e.CanExecute = false;
        }
    }
}
